home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / program / mc.lha / MC / multicolor.h < prev    next >
C/C++ Source or Header  |  1995-11-06  |  13KB  |  497 lines

  1. /******************************************************************************
  2. **                                         **
  3. ** MultiColor-Include                                 **
  4. **                                         **
  5. **---------------------------------------------------------------------------**
  6. ** V2.0 vom 01.10.95                                 **
  7. ******************************************************************************/
  8.  
  9. /* Prototypes für Libraryfunctions */
  10.  
  11. #include <proto/exec.h>
  12. #include <proto/graphics.h>
  13. #include <proto/intuition.h>
  14.  
  15. /* Includes */
  16.  
  17. #include <exec/types.h>
  18. #include <exec/exec.h>
  19. #include <exec/lists.h>
  20. #include <exec/memory.h>
  21. #include <exec/nodes.h>
  22. #include <graphics/gfx.h>
  23. #include <graphics/gfxmacros.h>
  24. #include <graphics/display.h>
  25. #include <graphics/displayinfo.h>
  26. #include <intuition/intuition.h>
  27. #include <intuition/intuitionbase.h>
  28. #include <intuition/screens.h>
  29. #include <math.h>
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <time.h>
  34.  
  35. /* die entsprechenden Matheroutinen includen */
  36.  
  37. #ifdef _M68881
  38.     #include <m68881.h>
  39. #endif
  40.  
  41. /* Definitionen */
  42.  
  43. typedef struct
  44. {
  45.     ULONG   Red,Green,Blue;
  46. } Triplet_32;
  47.  
  48. typedef struct
  49. {
  50.     WORD        NumColors;
  51.     WORD        FirstColor;
  52.     Triplet_32        Triplet[256];
  53.     WORD        Terminator;
  54. } Palette_32;
  55.  
  56. typedef struct
  57. {
  58.     WORD r,g,b;
  59. } MCPoint;
  60.  
  61. void MC_PrintPoint (MCPoint * point)
  62. {
  63.     printf ("Point [%5d:%5d:%5d]\n"
  64.     , point->r
  65.     , point->g
  66.     , point->b
  67.     );
  68. }
  69.  
  70. typedef struct
  71. {
  72.     struct RastPort   rp[256];        /* One Rastport for each pen    */
  73.     struct ViewPort * vp;        /* The associated ViewPort        */
  74.     UBYTE          ix_r[86],     /* Pen for RED fraction of color    */
  75.               ix_g[86],     /* Pen for GREEN fraction of color    */
  76.               ix_b[86];     /* Pen for BLUE fraction of color    */
  77.     UBYTE          ix_back[256]; /* Pen to Color reversal        */
  78.     UWORD          xres,        /* Resolution in X (This is NOT
  79.                     screen->Width !!!)        */
  80.               yres;        /* Resolution in Y            */
  81.     Palette_32          palette;        /* The palette for the screen    */
  82.     MCPoint          error;        /* This contains the mismatch for
  83.                     the last few colors.        */
  84.     UBYTE          cfact;        /* Factor for Color->Pen        */
  85.     UWORD        * ymod;        /* [0..yres] Gives y%3        */
  86. } MCHandle;
  87.  
  88. /* Functions ************************************************************************************************/
  89.  
  90. MCHandle *MC_Init(struct Screen *scr,struct Window *win,UBYTE dep)
  91. {
  92.     register UWORD i,cnum;
  93.     MCHandle *mch=0l;
  94.     UBYTE cd[17];
  95.  
  96.     if(mch=AllocMem(sizeof(MCHandle),MEMF_ANY|MEMF_CLEAR))
  97.     {
  98.     mch->xres=scr->Width*2/3;
  99.     mch->yres=scr->Height>>1;
  100.  
  101.     if (!(mch->ymod = AllocMem(sizeof(UWORD)*mch->yres,MEMF_ANY)) )
  102.     {
  103.         FreeMem (mch,sizeof(MCHandle));
  104.         return NULL;
  105.     }
  106.  
  107.     for (i=cnum=0; i<mch->yres; i++)
  108.     {
  109.         mch->ymod[i] = cnum ++;
  110.  
  111.         if (cnum==3)
  112.         cnum=0;
  113.     }
  114.  
  115.     mch->vp=&scr->ViewPort;
  116.     for(i=0;i<(1L<<dep);i++)
  117.     {
  118.         mch->rp[i]=*win->RPort;
  119.         SetAPen(&(mch->rp[i]),i);
  120.     }
  121.  
  122.     mch->palette.Triplet[0].Red  =0l;
  123.     mch->palette.Triplet[0].Green=0l;
  124.     mch->palette.Triplet[0].Blue =0l;
  125.  
  126.     switch(dep)
  127.     {
  128.     case 4:         /* r,g,b  6 Abstufungen =>    216 Farben */
  129.         cd[1]=48;cd[2]=96;cd[3]=144;cd[4]=192;cd[5]=240;
  130.         cnum=1;
  131.         mch->ix_b[0]=mch->ix_g[0]=mch->ix_r[0]=0;
  132.         for(i=1;i<6;i++) { mch->ix_b[i]=cnum;cnum++; }
  133.         for(i=1;i<6;i++) { mch->ix_g[i]=cnum;cnum++; }
  134.         for(i=1;i<6;i++) { mch->ix_r[i]=cnum;cnum++; }
  135.  
  136.         cnum=1;
  137.         mch->ix_back[0]=0;
  138.         for(i=1;i<6;i++) { mch->ix_back[cnum]=i;cnum++; }
  139.         for(i=1;i<6;i++) { mch->ix_back[cnum]=i;cnum++; }
  140.         for(i=1;i<6;i++) { mch->ix_back[cnum]=i;cnum++; }
  141.  
  142.         cnum=1;
  143.         for(i=1;i<6;i++)
  144.         {
  145.         mch->palette.Triplet[cnum].Red    =0l;
  146.         mch->palette.Triplet[cnum].Green=0l;
  147.         mch->palette.Triplet[cnum].Blue =(ULONG)(0x01010101*cd[i]);
  148.         cnum++;
  149.         }
  150.         for(i=1;i<6;i++)
  151.         {
  152.         mch->palette.Triplet[cnum].Red    =0l;
  153.         mch->palette.Triplet[cnum].Green=(ULONG)(0x01010101*cd[i]);
  154.         mch->palette.Triplet[cnum].Blue =0l;
  155.         cnum++;
  156.         }
  157.         for(i=1;i<6;i++)
  158.         {
  159.         mch->palette.Triplet[cnum].Red    =(ULONG)(0x01010101*cd[i]);
  160.         mch->palette.Triplet[cnum].Green=0l;
  161.         mch->palette.Triplet[cnum].Blue =0l;
  162.         cnum++;
  163.         }
  164.         mch->palette.NumColors=16;
  165.         mch->cfact=5;
  166.         break;
  167.  
  168.     case 5:         /* r,g,b 11 Abstufungen =>   1331 Farben */
  169.         cd[1]=32;cd[2]=48;cd[3]=80;cd[4]=96;cd[5]=128;cd[6]=144;
  170.         cd[7]=176;cd[8]=192;cd[9]=224;cd[10]=240;
  171.         cnum=1;
  172.         mch->ix_b[0]=mch->ix_g[0]=mch->ix_r[0]=0;
  173.         for(i=1;i<11;i++) { mch->ix_b[i]=cnum;cnum++; }
  174.         for(i=1;i<11;i++) { mch->ix_g[i]=cnum;cnum++; }
  175.         for(i=1;i<11;i++) { mch->ix_r[i]=cnum;cnum++; }
  176.  
  177.         cnum=1;
  178.         mch->ix_back[0]=0;
  179.         for(i=1;i<11;i++) { mch->ix_back[cnum]=i;cnum++; }
  180.         for(i=1;i<11;i++) { mch->ix_back[cnum]=i;cnum++; }
  181.         for(i=1;i<11;i++) { mch->ix_back[cnum]=i;cnum++; }
  182.  
  183.         cnum=1;
  184.         for(i=1;i<11;i++)
  185.         {
  186.         mch->palette.Triplet[cnum].Red    =0l;
  187.         mch->palette.Triplet[cnum].Green=0l;
  188.         mch->palette.Triplet[cnum].Blue =(ULONG)(0x01010101*(i<<4));
  189.         cnum++;
  190.         }
  191.         for(i=1;i<11;i++)
  192.         {
  193.         mch->palette.Triplet[cnum].Red    =0l;
  194.         mch->palette.Triplet[cnum].Green=(ULONG)(0x01010101*(i<<4));
  195.         mch->palette.Triplet[cnum].Blue =0l;
  196.         cnum++;
  197.         }
  198.         for(i=1;i<11;i++)
  199.         {
  200.         mch->palette.Triplet[cnum].Red    =(ULONG)(0x01010101*(i<<4));
  201.         mch->palette.Triplet[cnum].Green=0l;
  202.         mch->palette.Triplet[cnum].Blue =0l;
  203.         cnum++;
  204.         }
  205.         mch->palette.Triplet[cnum].Red  =0xFFFFFFFF;
  206.         mch->palette.Triplet[cnum].Green=0xFFFFFFFF;
  207.         mch->palette.Triplet[cnum].Blue =0xFFFFFFFF;
  208.         mch->palette.NumColors=32;
  209.         mch->cfact=10;
  210.         break;
  211.  
  212.     case 6:         /* r,g,b 16 Abstufungen =>   4096 Farben */
  213.         cd[1]=32;cd[2]=48;cd[3]=128;cd[4]=144;cd[5]=160;cd[6]=176;
  214.         cd[7]=192;cd[8]=208;cd[9]=224;cd[10]=240;
  215.         mch->ix_b[0]=mch->ix_g[0]=mch->ix_r[0]=0;
  216.         mch->ix_b[ 1]=33;mch->ix_g[ 1]=43;mch->ix_r[ 1]=53;
  217.         mch->ix_b[ 2]= 1;mch->ix_g[ 2]=11;mch->ix_r[ 2]=21;
  218.         mch->ix_b[ 3]= 2;mch->ix_g[ 3]=12;mch->ix_r[ 3]=22;
  219.         mch->ix_b[ 4]=36;mch->ix_g[ 4]=46;mch->ix_r[ 4]=56;
  220.         mch->ix_b[ 5]=38;mch->ix_g[ 5]=48;mch->ix_r[ 5]=58;
  221.         mch->ix_b[ 6]=40;mch->ix_g[ 6]=50;mch->ix_r[ 6]=60;
  222.         mch->ix_b[ 7]=42;mch->ix_g[ 7]=52;mch->ix_r[ 7]=62;
  223.         mch->ix_b[ 8]= 3;mch->ix_g[ 8]=13;mch->ix_r[ 8]=23;
  224.         mch->ix_b[ 9]= 4;mch->ix_g[ 9]=14;mch->ix_r[ 9]=24;
  225.         mch->ix_b[10]= 5;mch->ix_g[10]=15;mch->ix_r[10]=25;
  226.         mch->ix_b[11]= 6;mch->ix_g[11]=16;mch->ix_r[11]=26;
  227.         mch->ix_b[12]= 7;mch->ix_g[12]=17;mch->ix_r[12]=27;
  228.         mch->ix_b[13]= 8;mch->ix_g[13]=18;mch->ix_r[13]=28;
  229.         mch->ix_b[14]= 9;mch->ix_g[14]=19;mch->ix_r[14]=29;
  230.         mch->ix_b[15]=10;mch->ix_g[15]=20;mch->ix_r[15]=30;
  231.  
  232.         cnum=1;
  233.         mch->ix_back[ 0]= 0;
  234.         mch->ix_back[33]=mch->ix_back[43]=mch->ix_back[53]= 1;
  235.         mch->ix_back[ 1]=mch->ix_back[11]=mch->ix_back[21]= 2;
  236.         mch->ix_back[ 2]=mch->ix_back[12]=mch->ix_back[22]= 3;
  237.         mch->ix_back[36]=mch->ix_back[46]=mch->ix_back[56]= 4;
  238.         mch->ix_back[38]=mch->ix_back[48]=mch->ix_back[58]= 5;
  239.         mch->ix_back[40]=mch->ix_back[50]=mch->ix_back[60]= 6;
  240.         mch->ix_back[42]=mch->ix_back[52]=mch->ix_back[62]= 7;
  241.         mch->ix_back[ 3]=mch->ix_back[13]=mch->ix_back[23]= 8;
  242.         mch->ix_back[ 4]=mch->ix_back[14]=mch->ix_back[24]= 9;
  243.         mch->ix_back[ 5]=mch->ix_back[15]=mch->ix_back[25]=10;
  244.         mch->ix_back[ 6]=mch->ix_back[16]=mch->ix_back[26]=11;
  245.         mch->ix_back[ 7]=mch->ix_back[17]=mch->ix_back[27]=12;
  246.         mch->ix_back[ 8]=mch->ix_back[18]=mch->ix_back[28]=13;
  247.         mch->ix_back[ 9]=mch->ix_back[19]=mch->ix_back[29]=14;
  248.         mch->ix_back[10]=mch->ix_back[20]=mch->ix_back[30]=15;
  249.  
  250.         cnum=1;
  251.         for(i=1;i<11;i++)
  252.         {
  253.         mch->palette.Triplet[cnum].Red    =0l;
  254.         mch->palette.Triplet[cnum].Green=0l;
  255.         mch->palette.Triplet[cnum].Blue =(ULONG)(0x01010101*cd[i]);
  256.         cnum++;
  257.         }
  258.         for(i=1;i<11;i++)
  259.         {
  260.         mch->palette.Triplet[cnum].Red    =0l;
  261.         mch->palette.Triplet[cnum].Green=(ULONG)(0x01010101*cd[i]);
  262.         mch->palette.Triplet[cnum].Blue =0l;
  263.         cnum++;
  264.         }
  265.         for(i=1;i<11;i++)
  266.         {
  267.         mch->palette.Triplet[cnum].Red    =(ULONG)(0x01010101*cd[i]);
  268.         mch->palette.Triplet[cnum].Green=0l;
  269.         mch->palette.Triplet[cnum].Blue =0l;
  270.         cnum++;
  271.         }
  272.         mch->palette.Triplet[cnum].Red  =0xFFFFFFFF;
  273.         mch->palette.Triplet[cnum].Green=0xFFFFFFFF;
  274.         mch->palette.Triplet[cnum].Blue =0xFFFFFFFF;
  275.         mch->palette.NumColors=32;
  276.         mch->cfact=15;
  277.         break;
  278.  
  279.     case 8:         /* r,g,b 85 Abstufungen => 614125 Farben */
  280.         cnum=1;
  281.         mch->ix_b[0]=0;
  282.         for(i=1;i<85;i++) { mch->ix_b[i]=cnum;cnum++; }
  283.         mch->ix_g[0]=0;
  284.         for(i=1;i<85;i++) { mch->ix_g[i]=cnum;cnum++; }
  285.         mch->ix_r[0]=0;
  286.         for(i=1;i<85;i++) { mch->ix_r[i]=cnum;cnum++; }
  287.  
  288.         cnum=1;
  289.         mch->ix_back[0]=0;
  290.         for(i=1;i<85;i++) { mch->ix_back[cnum]=i;cnum++; }
  291.         for(i=1;i<85;i++) { mch->ix_back[cnum]=i;cnum++; }
  292.         for(i=1;i<85;i++) { mch->ix_back[cnum]=i;cnum++; }
  293.  
  294.         cnum=1;
  295.         for(i=1;i<85;i++)
  296.         {
  297.         mch->palette.Triplet[cnum].Red    =0l;
  298.         mch->palette.Triplet[cnum].Green=0l;
  299.         mch->palette.Triplet[cnum].Blue =(ULONG)(0x01010101*(i*3));
  300.         cnum++;
  301.         }
  302.         for(i=1;i<85;i++)
  303.         {
  304.         mch->palette.Triplet[cnum].Red    =0l;
  305.         mch->palette.Triplet[cnum].Green=(ULONG)(0x01010101*(i*3));
  306.         mch->palette.Triplet[cnum].Blue =0l;
  307.         cnum++;
  308.         }
  309.         for(i=1;i<85;i++)
  310.         {
  311.         mch->palette.Triplet[cnum].Red    =(ULONG)(0x01010101*(i*3));
  312.         mch->palette.Triplet[cnum].Green=0l;
  313.         mch->palette.Triplet[cnum].Blue =0l;
  314.         cnum++;
  315.         }
  316.         mch->palette.Triplet[cnum].Red  =0xFFFFFFFF;
  317.         mch->palette.Triplet[cnum].Green=0xFFFFFFFF;
  318.         mch->palette.Triplet[cnum].Blue =0xFFFFFFFF;
  319.         mch->palette.NumColors=256;
  320.         mch->cfact=84;
  321.         break;
  322.     } /* switch */
  323.  
  324.     mch->palette.FirstColor=0;
  325.     mch->palette.Terminator=0;
  326.     LoadRGB32(mch->vp,(ULONG *)&(mch->palette));
  327.  
  328.     mch->error.r=mch->error.g=mch->error.b=0;
  329.     }
  330.     return(mch);
  331. }
  332.  
  333. void MC_Free(MCHandle *mch)
  334. {
  335.     if(mch)
  336.     FreeMem(mch,sizeof(MCHandle));
  337. }
  338.  
  339. /***********************************************************************************************************/
  340.  
  341. #ifndef MC_PATTERN
  342. #   define MC_PATTERN 2
  343. #endif
  344.  
  345. /* Offsets for the pixels. Every position is mapped to three distinct
  346.     points. */
  347. static UWORD MC_Coords[3][2][6] =
  348. {
  349.     /* Pattern 1 (original) */
  350. #if MC_PATTERN==1
  351.     /*    GB R
  352.     R GB
  353.  
  354.     RG B
  355.     B RG
  356.  
  357.     BR G
  358.     G BR */
  359. { /* y==0 */
  360.     { /* x==0 */
  361.     /*R:*/ 0,1, /*G:*/ 0,0, /*B*/ 1,0,
  362.     },
  363.     { /* x==1 */
  364.     /*R:*/ 1,0, /*G:*/ 0,1, /*B*/ 1,1,
  365.     }
  366. },
  367. { /* y==1 */
  368.     { /* x==0 */
  369.     /*R:*/ 0,0, /*G:*/ 1,0, /*B*/ 0,1,
  370.     },
  371.     { /* x==1 */
  372.     /*R:*/ 0,1, /*G:*/ 1,1, /*B*/ 1,0,
  373.     }
  374. },
  375. { /* y==2 */
  376.     { /* x==0 */
  377.     /*R:*/ 1,0, /*G:*/ 0,1, /*B*/ 0,0,
  378.     },
  379.     { /* x==1 */
  380.     /*R:*/ 1,1, /*G:*/ 1,0, /*B*/ 0,1,
  381.     }
  382. }
  383. #elif MC_PATTERN==2
  384.     /*    RG B
  385.     B RG
  386.  
  387.     GB R
  388.     R GB
  389.  
  390.     BR G
  391.     G BR */
  392. { /* y==0 */
  393.     { /* x==0 */
  394.     /*R:*/ 0,0, /*G:*/ 1,0, /*B*/ 0,1,
  395.     },
  396.     { /* x==1 */
  397.     /*R:*/ 0,1, /*G:*/ 1,1, /*B*/ 1,0,
  398.     }
  399. },
  400. { /* y==1 */
  401.     { /* x==0 */
  402.     /*R:*/ 0,1, /*G:*/ 0,0, /*B*/ 1,0,
  403.     },
  404.     { /* x==1 */
  405.     /*R:*/ 1,0, /*G:*/ 0,1, /*B*/ 1,1,
  406.     }
  407. },
  408. { /* y==2 */
  409.     { /* x==0 */
  410.     /*R:*/ 1,0, /*G:*/ 0,1, /*B*/ 0,0,
  411.     },
  412.     { /* x==1 */
  413.     /*R:*/ 1,1, /*G:*/ 1,0, /*B*/ 0,1,
  414.     }
  415. }
  416. #elif MC_PATTERN==3
  417.     /*    GB G
  418.     R RB
  419.  
  420.     GB G
  421.     R RB
  422.  
  423.     GB G
  424.     R RB */
  425. { /* y==0 */
  426.     { /* x==0 */
  427.     /*R:*/ 0,1, /*G:*/ 0,0, /*B*/ 1,0,
  428.     },
  429.     { /* x==1 */
  430.     /*R:*/ 0,1, /*G:*/ 1,0, /*B*/ 1,1,
  431.     }
  432. },
  433. { /* y==1 */
  434.     { /* x==0 */
  435.     /*R:*/ 0,1, /*G:*/ 0,0, /*B*/ 1,0,
  436.     },
  437.     { /* x==1 */
  438.     /*R:*/ 0,1, /*G:*/ 1,0, /*B*/ 1,1,
  439.     }
  440. },
  441. { /* y==2 */
  442.     { /* x==0 */
  443.     /*R:*/ 0,1, /*G:*/ 0,0, /*B*/ 1,0,
  444.     },
  445.     { /* x==1 */
  446.     /*R:*/ 0,1, /*G:*/ 1,0, /*B*/ 1,1,
  447.     }
  448. }
  449. #endif
  450. };
  451.  
  452. void MC_PutPixel(MCHandle *mch,UWORD x,UWORD y,MCPoint *pt)
  453. {
  454.     UWORD rx,ry;
  455.     UBYTE r,g,b;
  456.     UWORD t,*cptr;
  457.     struct RastPort * rp = mch->rp;
  458.  
  459.     rx=x+(x>>1);
  460.     ry=y<<1;
  461.  
  462.     t=pt->r*mch->cfact+mch->error.r; mch->error.r=(BYTE)t&0xFF; r=t>>8;
  463.     t=pt->g*mch->cfact+mch->error.g; mch->error.g=(BYTE)t&0xFF; g=t>>8;
  464.     t=pt->b*mch->cfact+mch->error.b; mch->error.b=(BYTE)t&0xFF; b=t>>8;
  465.  
  466.     r = mch->ix_r[r];
  467.     g = mch->ix_g[g];
  468.     b = mch->ix_b[b];
  469.  
  470.     cptr = MC_Coords[mch->ymod[y]][x&1];
  471.  
  472.     WritePixel (&rp[r],rx+cptr[0],ry+cptr[1]);
  473.     WritePixel (&rp[g],rx+cptr[2],ry+cptr[3]);
  474.     WritePixel (&rp[b],rx+cptr[4],ry+cptr[5]);
  475. }
  476.  
  477. void MC_GetPixel(MCHandle *mch,UWORD x,UWORD y,MCPoint *akt)
  478. {
  479.     UWORD rx,ry,*cptr;
  480.     UBYTE r,g,b;
  481.     struct RastPort * rp = &mch->rp[0];
  482.  
  483.     r = g = b = 0;
  484.     rx = x + (x >> 1);
  485.     ry = y << 1;
  486.  
  487.     cptr = MC_Coords[mch->ymod[y]][x&1];
  488.  
  489.     r=ReadPixel (rp,rx+cptr[0],ry+cptr[1]);
  490.     g=ReadPixel (rp,rx+cptr[2],ry+cptr[3]);
  491.     b=ReadPixel (rp,rx+cptr[4],ry+cptr[5]);
  492.  
  493.     akt->r=(mch->ix_back[r]<<8)/mch->cfact;
  494.     akt->g=(mch->ix_back[g]<<8)/mch->cfact;
  495.     akt->b=(mch->ix_back[b]<<8)/mch->cfact;
  496. }
  497.